package com.bolt.auth.security.admin.biz.quartz.controller;


import com.bolt.auth.admin.api.quartz.dto.JobDTO;
import com.bolt.auth.admin.api.quartz.dto.JobQueryCriteria;
import com.bolt.auth.admin.api.quartz.dto.converter.JobConverter;
import com.bolt.auth.admin.api.quartz.entity.JobEntity;
import com.bolt.auth.security.admin.biz.quartz.service.JobService;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/api/sys/quartz")
@RequiredArgsConstructor
@Api(description = "定时任务管理")
public class JobController {

    private final JobService jobService;

    /**
     * 定时任务日志列表分页
     *
     * @return
     */
    @GetMapping
    public ResultMessage list(Pageable pageable, JobQueryCriteria dto) {
        PageInfo<JobEntity> pageInfo = jobService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dto, criteriaBuilder));
        return Results.success(pageInfo.map(K -> JobConverter.toDTO(K)));
    }


    @PostMapping
    @ApiOperation(notes = "定时任务新增", httpMethod = "POST", value = "定时任务新增")
    public ResultMessage<Void> create(@Validated @RequestBody JobDTO jobDTO) {
        jobService.saveOrUpdate(jobDTO);
        return Results.success();
    }

    @PutMapping
    @ApiOperation(notes = "定时任务编辑", httpMethod = "PUT", value = "定时任务编辑")
    public ResultMessage<Void> update(@Validated(JobDTO.Update.class) @RequestBody JobDTO dao) {
        jobService.saveOrUpdate(dao);
        return Results.success();
    }
    /**
     * 数据字典删除操作
     */
    @DeleteMapping
    @ApiOperation(notes = "根据ID删除数据字典", httpMethod = "POST", value = "根据ID删除数据字典")
    public ResultMessage<Void> doDelete(@RequestBody Set <Long> ids) {
        for (Long id : ids) {
            jobService.deleteById(id);
        }
        return Results.success();
    }


}

 